package com.example.demo.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.reporters.Summariser;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;

import java.io.File;
import java.io.IOException;
@Slf4j
public class JMeterTestUtils {

    public static void JMeterRun(String fileName){
        try {
            JMeterUtils.setJMeterHome("D:\\work\\apache-jmeter-5.4.3\\apache-jmeter-5.4.3");
            JMeterUtils.loadJMeterProperties("D:\\work\\apache-jmeter-5.4.3\\apache-jmeter-5.4.3\\bin\\jmeter.properties");
            JMeterUtils.initLocale();
            SaveService.loadProperties();
            HashTree hashTree = SaveService.loadTree(new File(fileName));
            Summariser summariser =null;
            String summariseName = JMeterUtils.getPropDefault("summariser.name","summary");
            if (summariseName.length()>0){
                summariser = new Summariser(summariseName);
            }
            ResultCollector resultCollector =new ResultCollector(summariser);
            hashTree.add(hashTree.getArray(),resultCollector);
            StandardJMeterEngine jmeter = new StandardJMeterEngine();
            jmeter.configure(hashTree);
            jmeter.run();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
